package operation;

import book.Book;
import book.BookList;

import javax.imageio.metadata.IIOMetadataController;
import java.util.Scanner;

public class DelOperation implements Ioperateion {
    @Override
    public void work(BookList books) {
        System.out.println("删除图书！");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你要删除的书");
        String name = scanner.nextLine();
        int currentSize = books.getUsedSize();

        int delIndex = -1;

        for (int i = 0; i < currentSize; i++) {
            Book book = books.getBook(i);
            if (book.getName().equals(name)) {
                delIndex = i;
                break;
            }
        }
//        if (i == currentSize) {
//            System.out.println("没有要删除的这本书");
//            return;
//        }
        if (delIndex == -1) {
            System.out.println("没有要删除的这本书");
            return;
        }

        for (int j = delIndex; j < currentSize - 1; j++) {
            //让j+1下标给j下标位置的值
            Book book = books.getBook(j + 1);
            books.setBooks(j, book);
        }
        books.setBooks(currentSize - 1, null);
        books.setUsedSize(currentSize - 1);
        System.out.println("删除图书成功");
    }
}
